package com.course.android.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.view.View;

import com.baselib.android.event.EventManager;
import com.baselib.android.utils.ToastTool;
import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.course.android.R;
import com.course.android.activity.base.BaseActivity;
import com.course.android.activity.base.BaseNetActivity;
import com.course.controller.UploadController;
import com.course.model.HttpConfigBean;

import java.io.File;
import java.util.List;

/**
 * Created by User on 2018/9/26.
 */

public class CameraVideoActivity extends BaseNetActivity {

    JCameraView jCameraView;


    @Override
    protected void onActivityResultCallBack(Integer reqCode, Intent data) {

    }

    @Override
    public void initView() {
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
        jCameraView = (JCameraView) findViewById(R.id.jcameraview);
        jCameraView.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");
//        jCameraView.setFeatures(getIntent().getBooleanExtra("canRecord",true)?JCameraView.BUTTON_STATE_BOTH:JCameraView.BUTTON_STATE_ONLY_CAPTURE);
        jCameraView.setFeatures(JCameraView.BUTTON_STATE_BOTH);
//设置视频质量
        jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
//JCameraView监听
        jCameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {
                //打开Camera失败回调
            }

            @Override
            public void AudioPermissionError() {
                //没有录取权限回调
            }
        });

        jCameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(final Bitmap bitmap) {
                //获取图片bitmap

            }

            @Override
            public void recordSuccess(final String url, final Bitmap firstFrame) {
                //获取视频路径
                UploadController.getInstance().uploadFils(url, firstFrame, CameraVideoActivity.this);
            }
        });
//左边按钮点击事件
        jCameraView.setLeftClickListener(new ClickListener() {
            @Override
            public void onClick() {
                CameraVideoActivity.this.finish();
            }
        });
//右边按钮点击事件
        jCameraView.setRightClickListener(new ClickListener() {
            @Override
            public void onClick() {

            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_camera;
    }


    @Override
    public <T> void onData(HttpConfigBean flag, T o) {
        List<String> urls = (List<String>) o;
        EventManager.getInstance().postSticky(urls);
        finish();
    }

    @Override
    public <T> void onPageData(HttpConfigBean flag, int pageNum, int pageSize, int total, List<T> array) {

    }
}
